#include "hdf_slist.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct Student
{
    struct HdfSListNode node;
    char name[50];
    int age;

}Student;


//迭代器版本或者普通版本都需要测试到 
bool test_slist_insert_head()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
    Student s2 = { NULL,"lily",21 };
    Student s3 = { NULL,"tom",22 };
	
	HdfSListInit(&list);
	
	HdfSListAdd(&list,&s1.node);
	HdfSListAdd(&list,&s2.node);
	HdfSListAdd(&list,&s3.node);
	
	
	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	
	return true;
}
bool test_slist_insert_tail()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
    Student s2 = { NULL,"lily",21 };
    Student s3 = { NULL,"tom",22 };
	
	
	//初始化单链表
	HdfSListInit(&list);
	
	HdfSListAddTail(&list,&s1.node);
	HdfSListAddTail(&list,&s2.node);
	HdfSListAddTail(&list,&s3.node);
	
	
	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	return true;
}

bool addComparerStudent(struct HdfSListNode * firstNode, struct HdfSListNode * secondNode)
{
	Student* fs = (Student *)firstNode;
	Student* ss = (Student *)secondNode;
	
	return fs->age >= ss->age;
}
//按照一定顺序插入链表节点
bool test_slist_insert_mid()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",24 };
  Student s2 = { NULL,"lily",21 };
  Student s3 = { NULL,"tom",23 };
	//初始化单链表
	HdfSListInit(&list);
	HdfSListAdd(&list,&s1.node);
	HdfSListAddOrder(&list,&s2.node,addComparerStudent);
	HdfSListAddOrder(&list,&s3.node,addComparerStudent);

	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	return true;
}
bool test_slist_remove_head()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
    Student s2 = { NULL,"lily",21 };
    Student s3 = { NULL,"tom",22 };
	
	//初始化单链表
	HdfSListInit(&list);
	
	
	HdfSListAdd(&list,&s1.node);
	HdfSListAdd(&list,&s2.node);
	HdfSListAdd(&list,&s3.node);
	//删除第一个节点前
	printf("删除第一个节点前\n");
	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	//删除第一个节点
	struct HdfSListNode *deleteNode = HdfSListPop(&list);
	
	//删除第一个节点后
	printf("删除第一个节点后\n");
	tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	return true;
}
bool test_slist_remove_tail()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
    Student s2 = { NULL,"lily",21 };
    Student s3 = { NULL,"tom",22 };
	
	
	//初始化单链表
	HdfSListInit(&list);
	
	
	HdfSListAdd(&list,&s1.node);
	HdfSListAdd(&list,&s2.node);
	HdfSListAdd(&list,&s3.node);
	//删除第一个节点前
	printf("删除最后一个节点前\n");
	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	
	//删除最后一个节点
	//找到最后一个节点
	struct HdfSListNode *lastNode = HdfSListGetLast(&list);
	//删除最后一个节点
	HdfSListRemove(&list,lastNode);
	
	//删除最后一个节点后
	printf("删除最后一个节点后\n");
	tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	
	return true;
}
//删除中间节点
bool test_slist_remove_mid()
{
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
    Student s2 = { NULL,"lily",21 };
    Student s3 = { NULL,"tom",22 };
	

	//初始化单链表
	HdfSListInit(&list);
	
	
	HdfSListAdd(&list,&s1.node);
	HdfSListAdd(&list,&s2.node);
	HdfSListAdd(&list,&s3.node);
	
	//删除中间节点前
	printf("删除中间节点前\n");
	struct HdfSListNode *tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	
	//删除中间节点
	int count = HdfSListCount(&list);
	struct HdfSListNode *rmPreNode = HdfSListPeek(&list);
	for(int i=0;i<count/2&& tempNode!=NULL;i++)
	{
		rmPreNode=HdfSListNext(rmPreNode);
	}
	if(rmPreNode->next!=NULL)
	{
		HdfSListRemove(&list,rmPreNode->next);
	}
	
	//删除中间节点后
	printf("删除中间节点后\n");
	tempNode = HdfSListPeek(&list);
	while(tempNode!=NULL)
	{
		Student* s = (Student *)tempNode;
        printf("name: %s , age: %d\n", s->name, s->age);
		tempNode=HdfSListNext(tempNode);
	}
	return true;
}
//迭代器遍历单链表
bool test_slist_traverse()
{
	struct HdfSListIterator iterator;
	struct HdfSList list;
	Student s1 = { NULL,"lucy",20 };
  Student s2 = { NULL,"lily",21 };
  Student s3 = { NULL,"tom",22 };
	//初始化单链表
	HdfSListInit(&list);
	HdfSListAdd(&list,&s1.node);
	HdfSListAdd(&list,&s2.node);
	HdfSListAdd(&list,&s3.node);
	
	//初始化迭代器
	HdfSListIteratorInit(&iterator,&list);
	struct HdfSListNode *tempNode = NULL;
	while(HdfSListIteratorHasNext(&iterator))
	{
		tempNode = HdfSListIteratorNext(&iterator);
		Student* s = (Student *)tempNode;
    printf("name: %s , age: %d\n", s->name, s->age);
	}
	
	return true;
}

int main()
{
	//test_slist_insert_head();
  //test_slist_insert_tail();
	//test_slist_insert_mid();
	//test_slist_remove_head();
	test_slist_traverse();
	//test_slist_remove_tail();
	//test_slist_remove_mid();
	return 0;
}